package com.example.mp.proxy;

import com.example.mp.service.UserService;
import com.example.mp.service.impl.UserServiceImpl;
import lombok.extern.log4j.Log4j2;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author hf
 * @version 1.0.0
 * @ClassName MyInvocationHandler.java
 * @Description TODO
 * @createTime 2023年11月01日 14:57:27
 */
@Log4j2
public class MyInvocationHandler implements InvocationHandler {
    private Object target;

    public MyInvocationHandler(Object target) {
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        log.info("before method ...");
        Class<?>[] list = proxy.getClass().getInterfaces();
        System.out.println(list[0]);
        Object value = method.invoke(target, args);
        log.info("after method ...");
        return value;
    }

    public static void main(String[] args) {
        UserServiceImpl target = new UserServiceImpl();
        InvocationHandler handler = new MyInvocationHandler(target);
        UserService proxy = (UserService) Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), handler);
        proxy.sayHi("张三,");
    }
}
